import QtQuick
import QtQuick.Controls

Rectangle {
    id: uploadVersionWin
    width: 640
    height: 480
    clip: true
    visible: false
    color: mBackColor

    signal uploadVersionOver()

    property color mBackColor: "#34393e"
    property color mTextColor: "#e1e4e8"
    property color mContentColor: "#44484d"

    property var helpContent: qsTr(" \
1. 点击检查可查看设备当前固件版本\n \
2. 点击升级可选择bin文件进行固件升级\n \
3. 以上操作请确保与设备的网络连接，设备状态可在主页右下角查看 \
")


    Connections {
        target: myHttp

        // 绑定http上传进度变化信号
        function onUploadProgressChanged(suffix, sendBytes, allBytes) {
            if(uploadVersionWin.visible) {
                if(myHandle.strContains(suffix, "updata")) {
                    // 设置进度条
                    if(sendBytes !== allBytes) {
                        progressBar.cpbValue = sendBytes / allBytes
                    } else {
                        progressBar.cpbValue = 1;
                    }
                }
            }
        }

        function onRequestFinished(ret) {
            if(uploadVersionWin.visible) {
                var strList = ["", ""]
                strList = myHandle.httpResultHandle(ret, false)
                if(myHandle.strContains(strList[0], "versions")) {
                    deviceVersions.inputText = strList[1]
                } else if(myHandle.strContains(strList[0], "updata")) {

                }
            }
        }
    }

    CustomHeaderBarClose {
        id: headerBar
        x: 0
        y: 0
        width: parent.width
        height: 34
        chbTitleText: qsTr("固件升级")
        chbHelpText: helpContent

        onClosePageBtnClicked: {
            parent.visible = false
        }
    }

    Column {
        anchors.top: headerBar.bottom
        anchors.bottom: progressBar.top
        width: parent.width
        spacing: 20
        padding: 20

        CustomBorderTitle {
            id: cbtUploadEffect
            width: parent.width - parent.padding * 2
            height: 70
            radius: 10
            cbtTitle: qsTr("检查版本")
            cbtTitleBackColor: mBackColor
            cbtTitleX: 30
            cbtFontSize: 14
            cbtTitleColor: mTextColor
            color: mContentColor

            Row {
                x: 10
                y: 20
                width: parent.width
                height: 30
                spacing: 20

                TitleTextInput {
                    id: deviceVersions
                    width: 120
                    height: parent.height
                    titleText: qsTr("版本号:")
                    titleWidth: 60
                    textColor: mTextColor
                    textSize: 14
                    inputBackColor: mContentColor
                    inputBorderWidth: 0
                    inputText: "0"
                    inputValidator: RegularExpressionValidator { regularExpression: /[0-9]{1,3}/ } // 文本验证器 仅可输入数字
                }

                AsButton {
                    id: checkUpdateBtn
                    width: 60
                    height: parent.height
                    text: qsTr("检查")
                    font.pixelSize: 14
                    textColor: mTextColor
                    backColor: mContentColor
                    itemRadius: 8

                    onClicked: {
                        myHandle.getDeviceVersions()
                    }
                }

                AsButton {
                    id: updateBtn
                    width: 60
                    height: parent.height
                    text: qsTr("升级")
                    font.pixelSize: 14
                    textColor: mTextColor
                    backColor: mContentColor
                    itemRadius: 8

                    onClicked: {
                        progressBar.reset()
                        myHandle.updateDeviceVersions()
                    }
                }
            }
        }
    }

    CustomProgressBar1 {
        id: progressBar
        width: parent.width
        height: 16
        anchors.bottom: parent.bottom
        cpbForeGradient: 100
        cpbBackColor: mContentColor
        cpbTextColor: "#FFFFFF"

        onAnimationOver: {
            uploadVersionOver()
        }
    }
}
